package weaver.interfaces.xfjt.action;

import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.xfjt.utils.XfjtUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;


/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-10-10
 * @Description: 5.物资比价流程归档，根据物品名称更新，(需要单独处理比价单位字段，需要拼接价格及换行),
 * 比价信息有效期表中比较有效日期更新/插入写入最新比价信息
 * @ModifyLog:
 **/
public class WeaWzbjlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        int userid = requestInfo.getRequestManager().getUser().getUID();
        writeLog(">>>>>WeaWzbjlcAction>>>>>requestid="+requestid);
        RecordSet rs = new RecordSet();
        try {
            rs.executeQuery("select id,bjdw,bjrq,bjlb from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String bjdw = Util.null2String(rs.getString("bjdw"));
            String bjrq = Util.null2String(rs.getString("bjrq"));
            String bjlb = Util.null2String(rs.getString("bjlb"));
            String [] strs = bjdw.split( "," );
            String bjyxxq = XfjtUtil.getBjrqyxq();

            rs.executeQuery("select * from "+tablename+"_dt1 where mainid=? and isnull(cgjg,0)>0",mainid);
            while (rs.next()) {
                Map requestMainMap = new HashMap<>();
                String mc = Util.null2String(rs.getString("mc"));
                String mcwb = Util.null2String(rs.getString("mcwb"));
                String lb = Util.null2String(rs.getString("lb"));
                String dw = Util.null2String(rs.getString("dw"));
                String qdcgdw = Util.null2String(rs.getString("qdcgdw"));
                String cgjg = Util.null2String(rs.getString("cgjg"));
                double bjdw1 = Util.getDoubleValue(rs.getString("bjdw1"));
                double bjdw2 = Util.getDoubleValue(rs.getString("bjdw2"));
                double bjdw3 = Util.getDoubleValue(rs.getString("bjdw3"));
                double bjdw4 = Util.getDoubleValue(rs.getString("bjdw4"));
                double bjdw5 = Util.getDoubleValue(rs.getString("bjdw5"));
                double bjdw6 = Util.getDoubleValue(rs.getString("bjdw6"));
                StringBuilder bjdws = new StringBuilder(  );
                for (int i=0;i<strs.length;i++){
                    String newbjdw =  XfjtUtil.getBjdwname(strs[i]);

                    if(bjdw1>=0 && i==0){
                        bjdws.append( newbjdw+"("+bjdw1+")<br>");
                    }
                    if(bjdw2>=0 && i==1){
                        bjdws.append( newbjdw+"("+bjdw2+")<br>");
                    }
                    if(bjdw3>=0 && i==2){
                        bjdws.append( newbjdw+"("+bjdw3+")<br>");
                    }
                    if(bjdw4>=0 && i==3){
                        bjdws.append( newbjdw+"("+bjdw4+")<br>");
                    }
                    if(bjdw5>=0 && i==4){
                        bjdws.append( newbjdw+"("+bjdw5+")<br>");
                    }
                    if(bjdw6>=0 && i==5){
                        bjdws.append( newbjdw+"("+bjdw6+")<br>");
                    }

                }
                rs.writeLog( "bjdws="+bjdws.toString() );
                if (XfjtUtil.isRepeatZxbj(mc)) {
                    requestMainMap.put("bjlc",requestid);
                    requestMainMap.put("bjrq",bjrq);
                    requestMainMap.put("bjlb",bjlb);
                    requestMainMap.put("lb",lb);
                    requestMainMap.put("mc",mc);
                    requestMainMap.put("mcwb",mcwb);
                    requestMainMap.put("dw",dw);
                    requestMainMap.put("bjdw",bjdws);
                    requestMainMap.put("qdcgdw",qdcgdw);
                    requestMainMap.put("cgjg",cgjg);
                    requestMainMap.put("zt",0);
                    requestMainMap.put("bjxxyxq",bjyxxq);  
                    XfjtUtil.insertModelData("uf_zxbjxx",userid,requestMainMap);
                 }else {
                    rs.executeUpdate("update uf_zxbjxx set bjxxyxq='"+bjyxxq+"',bjdw='"+bjdws.toString()+"'," +
                            " bjrq='"+bjrq+"',qdcgdw='"+qdcgdw+"',cgjg='"+cgjg+"',zt=0" +
                            " where mc=? and lb=?",mc,lb);
                }

            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
